
package com.fire.control.system.module.role.service.impl;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.fire.control.system.dto.ResourceThreeDTO;
import com.fire.control.system.dto.UserDTO;
import com.fire.control.system.entity.Resource;
import com.fire.control.system.entity.Role;
import com.fire.control.system.entity.User;
import com.fire.control.system.module.role.dao.RoleMapper;
import com.fire.control.system.module.role.service.IRoleService;
import com.fire.control.system.module.system.dao.ResourceRepository;
import com.fire.control.system.module.system.dao.RoleRepository;
import com.fire.control.system.module.user.dao.UserMapper;
import com.fire.control.system.module.user.dao.UserRepository;
import com.fire.control.system.statics.ResponseMessage;
import com.fire.control.system.support.protocol.comman.ProtocolSelector;
import com.fire.control.system.support.protocol.comman.ResponseProtocol;
import com.fire.control.system.support.protocol.role.RoleRequestProtocol;
import com.fire.control.system.support.util.ResourceTreeUtil;
import com.fire.control.system.support.util.XdaoDate;

@Service
public class RoleServiceImpl implements IRoleService {
	@Autowired
	private ProtocolSelector protocolSelector;
	@Autowired
	private RoleRepository roleRepository;
	@Autowired
	private ResourceRepository resourceRepository;
	@Autowired
	private UserMapper userMapper;
	@Autowired
	private UserRepository userRepository;
    @Autowired	
	private RoleMapper roleMapper;

	/**
	 * <p>
	 * Title: jumpToAddRole
	 * </p>
	 * <p>
	 * Description:
	 * </p>
	 * @param protocol
	 * @return
	 * @see com.fire.control.system.module.role.service.IRoleService#jumpToAddRole(com.fire.control.system.support.protocol.role.RoleRequestProtocol)
	 */
	@Override
	public ResponseProtocol getRoleAndResources(RoleRequestProtocol protocol,UserDTO u) {
		// TODO Auto-generated method stub
		protocol.setTaskuuid(UUID.randomUUID().toString());
		ResponseProtocol response = protocolSelector.choose(protocol);
		User user = userRepository.findOne(u.getUserId());
		Collection<Role> roles = user.getRole();
		//Collection<Role> allRoles = user.getRole();
		Set<Resource> resources = new HashSet<Resource>();
		
		Map<String,Object> returnMap = new HashMap<String,Object>();
		List<ResourceThreeDTO> resourceThreeDTO = new ArrayList<ResourceThreeDTO>();
		
		for (Role r : roles) {
			if (r != null && r.getStatus() == true) {
				resources.addAll(r.getResources());
			}
		}
		if (!resources.isEmpty() && null != resources) {
			resourceThreeDTO = ResourceTreeUtil.resourceDTOTree(resources);
			if("admin".equals(user.getLoginName())){
				roles = this.getAllRoles();
				returnMap.put("Roles", roles);
			}else{
				returnMap.put("Roles", roles);
			}
			returnMap.put("resourceThreeDTO", resourceThreeDTO);
			ResponseMessage.CODE_200.messageToProtocol(response).setMessage("数据返回成功!");;
			response.setData(returnMap);
		} else {
			ResponseMessage.CODE_400.messageToProtocol(response).setMessage("没有数据返回!");
		}
		return response;
	}

	/**
	 * <p>
	 * Title: saveRole
	 * </p>
	 * <p>
	 * Description: 添加角色
	 * </p>
	 * @param protocol
	 * @return
	 * @see com.fire.control.system.module.system.service.IAccountManagerService#saveRole(com.fire.control.system.support.protocol.account.AccountRequestProtocol)
	 */
	@Override
	public ResponseProtocol saveRole(RoleRequestProtocol protocol,UserDTO user) {
		// TODO Auto-generated method stub
		protocol.setTaskuuid(UUID.randomUUID().toString());
		ResponseProtocol response = protocolSelector.choose(protocol);
		
		//String[] resourcesId = protocol.getResourceId();
		//Collection<Resource> resources = new ArrayList<Resource>();
		Role r = protocol.getR();
		//Resource resource = null;
		
		User u = userRepository.findOne(user.getUserId());
		/*for (String id : resourcesId) {
			resource = resourceRepository.findOne(id);
			resources.add(resource);
		}*/
		r.setId(UUID.randomUUID().toString());
		//r.setResources(resources);
		
		r.setCompany(u.getCompany());
		
		Role returnRole = roleRepository.saveAndFlush(r);
		if (null != returnRole) {
			ResponseMessage.CODE_200.messageToProtocol(response).setMessage("角色添加成功!");;
			response.setDate(new XdaoDate());
			response.setData(returnRole);
		} else {
			ResponseMessage.CODE_400.messageToProtocol(response).setMessage("角色添加失败!");
			response.setDate(new XdaoDate());
		}
		return response;
	}

	/**
	 * <p>
	 * Title: getRole
	 * </p>
	 * <p>
	 * Description: 查询角色资源
	 * </p>
	 * @param protocol
	 * @return
	 * @see com.fire.control.system.module.system.service.IAccountManagerService#saveRole(com.fire.control.system.support.protocol.account.AccountRequestProtocol)
	 */
	@Override
	public Set<Resource> getResourceByRoleId(List<String> roleId) {
		// TODO Auto-generated method stub
		
		Set<Resource> resources = new HashSet<Resource>();
		
		for(String rid : roleId){
			resources.addAll(roleMapper.resourceList(rid));
		}
		
		if (null != resources && resources.size() > 0) {
			return resources;
		} else {
			return null;
		}
		
	}

	/**
	 * <p>
	 * Title: removeRole
	 * </p>
	 * <p>
	 * Description: 删除角色
	 * </p>
	 * @param protocol
	 * @return
	 * @see com.fire.control.system.module.system.service.IAccountManagerService#saveRole(com.fire.control.system.support.protocol.account.AccountRequestProtocol)
	 */
	@Override
	public ResponseProtocol removeRole(RoleRequestProtocol protocol,HttpServletRequest request) {
		// TODO Auto-generated method stub
		protocol.setTaskuuid(UUID.randomUUID().toString());
		ResponseProtocol response = protocolSelector.choose(protocol);
		String roleId = protocol.getRoleId();
		Role role = roleRepository.findOne(roleId);
		role.setStatus(false);
		Role returnRole = roleRepository.saveAndFlush(role);
		if (null != returnRole) {
			ResponseMessage.CODE_200.messageToProtocol(response).setMessage("角色删除成功!");;
			response.setDate(new XdaoDate());
			response.setData(returnRole);
		} else {
			ResponseMessage.CODE_400.messageToProtocol(response).setMessage("角色删除失败!");
			response.setDate(new XdaoDate());
		}
		return response;
	}

	/**
	 * <p>
	 * Title: roleAssignResources
	 * </p>
	 * <p>
	 * Description:修改角色权限
	 * </p>
	 * @param protocol
	 * @return
	 * @see com.fire.control.system.module.role.service.IRoleService#roleAssignResources(com.fire.control.system.support.protocol.role.RoleRequestProtocol)
	 */
	@Override
	public ResponseProtocol roleDistributionResource(RoleRequestProtocol protocol) {
		// TODO Auto-generated method stub
		protocol.setTaskuuid(UUID.randomUUID().toString());
		ResponseProtocol response = protocolSelector.choose(protocol);
		Collection<Resource> resources = new ArrayList<Resource>();
		String[] resourcesId = protocol.getResourceId();
		Role r = roleRepository.findOne(protocol.getId());
		for (String id : resourcesId) {
			Resource resource = resourceRepository.findOne(id);
			resources.add(resource);
		}
		r.setResources(resources);
		Role returnRole = roleRepository.saveAndFlush(r);
		if (null != returnRole) {
			ResponseMessage.CODE_200.messageToProtocol(response).setMessage("角色添加成功!");
			response.setDate(new XdaoDate());
			response.setData(returnRole);
		} else {
			ResponseMessage.CODE_400.messageToProtocol(response).setMessage("角色添加失败!");
			response.setDate(new XdaoDate());
		}
		return response;
	}

	/**
	 * <p>Title: getAllRoles</p>
	 * <p>Description: 获取系统中的所有角色</p>
	 * @return
	 * @see com.fire.control.system.module.role.service.IRoleService#getAllRoles()
	 */
	public List<Role> getAllRoles() {
		// TODO Auto-generated method stub
		List<Role> roleList = roleMapper.getAllRoles();
		if(!roleList.isEmpty() && roleList.size()>0){
			return roleList;
		}else{
			return null;
		}
	}
}
